package cc.comac.mom.queue;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cc.comac.mom.bean.BusLog;
import cc.comac.mom.bean.LogConstant;
import cc.comac.mom.service.LogAsyncService;
import cc.comac.mom.util.ConfigUtil;

/**
 * @ClassName: ConsumeBusLogQueue
 * @Description: 日志队列消费者
 * @author Administrator
 * @date 2018年8月29日
 *
 */
//@Component
public class ConsumeBusLogQueue {

	@Autowired
	private LogAsyncService logAsyncService;

	@PostConstruct
	public void startrtThread() {
//		int maxPool = Integer.parseInt(ConfigUtil.getValue(LogConstant.K_QUEUE_MAXPOOL));
		int maxPool = 10000;
		ExecutorService e = Executors.newFixedThreadPool(maxPool);// 线程池
		for (int i = 0; i < maxPool; i++) {
			e.submit(new PollBusLog(logAsyncService));
		}
	}

	class PollBusLog implements Runnable {
		LogAsyncService logAsyncService;

		public PollBusLog(LogAsyncService logAsyncService) {
			this.logAsyncService = logAsyncService;
		}

		@Override
		public void run() {
			while (true) {
				try {
					BusLog salesLog = BusLogQueue.getInstance().poll();
					if (salesLog != null) {
						logAsyncService.sendLog(salesLog);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}

	}
}
